using System;
using System.IO;
using System.Xml.Serialization;

namespace SoftwareMonkeys.Scriptech.Projects
{
	public class ProjectSaver
	{
		public static string SaveProject(string storeName, string groupName, Project project)
		{
			string projectPath = ProjectFileNamer.GetProjectPath(storeName, groupName, project.Name);
			
			if (!Directory.Exists(projectPath))
				Directory.CreateDirectory(projectPath);
			
			string projectPropertiesPath = ProjectFileNamer.GetProjectPropertiesPath(storeName, groupName, project.Name);
						
			SaveProjectToFile(projectPropertiesPath, project);
			
			string assembliesDirectoryPath = ProjectAssemblyFileNamer.CreateAssembliesDirectoryPath(storeName, groupName, project.Name, project.AssembliesDirectory);
			
			if (!Directory.Exists(assembliesDirectoryPath))
				Directory.CreateDirectory(assembliesDirectoryPath);
			
			return projectPropertiesPath;
		}
		

		public static void SaveProjectToFile (string filePath, Project project)
		{
			using (StreamWriter writer = File.CreateText (filePath)) {
				XmlSerializer serializer = new XmlSerializer (project.GetType());
				serializer.Serialize (writer, project);
				
			}
		}
	}
}

